package com.groupbuying.currency.consumer.service.impl;


import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.Producer;
import com.aliyun.openservices.shade.com.alibaba.fastjson.JSON;
import com.aliyun.openservices.shade.com.alibaba.fastjson.serializer.SerializerFeature;
import com.groupbuying.currency.consumer.connector.CustomerFeign;
import com.groupbuying.currency.consumer.connector.SysconfigFeign;
import com.groupbuying.currency.consumer.constants.CommonConstants;
import com.groupbuying.currency.consumer.constants.CurrencyConstants;
import com.groupbuying.currency.consumer.context.FilterContextHandler;
import com.groupbuying.currency.consumer.domain.CurrencyCustomerDO;
import com.groupbuying.currency.consumer.domain.CurrencyCustomerLogDO;
import com.groupbuying.currency.consumer.dto.mq.TokenPayInfoPublishingToMq;
import com.groupbuying.currency.consumer.mq.CurrencyMQConfig;
import com.groupbuying.currency.consumer.mq.CurrencyMQProducer;
import com.groupbuying.currency.consumer.service.CurrencyCustomerLogService;
import com.groupbuying.currency.consumer.service.CurrencyCustomerService;
import com.groupbuying.currency.consumer.service.CurrencyRebackService;
import com.groupbuying.currency.consumer.utils.BeanOrMapConvertUtils;
import com.groupbuying.currency.consumer.utils.DateUtils;
import com.groupbuying.currency.consumer.utils.Object2BigDecimal;
import com.groupbuying.currency.consumer.utils.apiresult.ApiResult;
import com.groupbuying.currency.consumer.utils.apiresult.ApiResultCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@Service
public class CurrencyRebackServiceImpl implements CurrencyRebackService {

    @Autowired
    private CurrencyCustomerService currencyCustomerService;

    @Autowired
    private CurrencyCustomerLogService currencyCustomerLogService;

    @Autowired
    private SysconfigFeign sysconfigFeign;

    @Autowired
    private CurrencyMQProducer currencyMQProducer;

    @Autowired
    private CustomerFeign customerFeign;

    @Override
    @Transactional
    public Boolean doRebackWithOnlinePay(CurrencyCustomerLogDO currencyCustomerLogDO, String customerId, String payId) {
        /* 变动积分账户 */
        CurrencyCustomerDO currencyCustomerDO = currencyCustomerService.get(customerId);

        BigDecimal useCurrency = currencyCustomerDO.getUseCurrency();

        currencyCustomerDO.setUseCurrency(useCurrency.subtract(currencyCustomerLogDO.getCurrencyCount()));
        currencyCustomerDO.setTotalCurrency(currencyCustomerDO.getUseCurrency().add(currencyCustomerDO.getFreezeCurrency()));
        currencyCustomerService.update(currencyCustomerDO);

        /* 3、记录积分消费日志（因该日志需入库故同步记录，doPayWithCurrency方法应提现事务性） */
        CurrencyCustomerLogDO currencyCustomerLogDO_back = new CurrencyCustomerLogDO();
        currencyCustomerLogDO_back.setLogId(sysconfigFeign.createLogID());
        currencyCustomerLogDO_back.setActivityId(null);
        currencyCustomerLogDO_back.setCreateTime(DateUtils.format(new Date(), DateUtils.YMDHMS));
        currencyCustomerLogDO_back.setPayId(currencyCustomerLogDO.getPayId());
        currencyCustomerLogDO_back.setCurrencyCount(currencyCustomerLogDO.getCurrencyCount());
        /* 碎银流向：01、活动；02、消费 ；03、积分退款 ；04、现金退款*/
        currencyCustomerLogDO_back.setCurrencySource(CurrencyConstants.CURRENCY_SOURCE_XJREFOUND);
        /* 碎银变化类型：01：增加：02减少，03：无变化 */
        currencyCustomerLogDO_back.setCurrencyType(CurrencyConstants.CURRENCY_TYPE_DECREASE);
        //currencyCustomerLogDO.setCustomerId(customerId);
        /* 删除标记 1-未删除，0-已删除 */
        currencyCustomerLogDO_back.setDelFlag(CommonConstants.COMMON_FLAG_UNDEL);
        currencyCustomerLogDO_back.setCustomerId(customerId);
        currencyCustomerLogDO_back.setCurrencyBalance(currencyCustomerDO.getTotalCurrency());
        currencyCustomerLogService.save(currencyCustomerLogDO_back);

        //发短信通知
        String msgContent = "您的现金支付订单退款成功，扣除" + currencyCustomerLogDO.getCurrencyCount() + "积分。";
        customerFeign.sendSmsWithCustomerId(customerId,msgContent);

        /* 通知支付中心 */
//        Producer producer = currencyMQProducer.getCurrencyMQProducer();

        /* 1、填充发送到支付中心的支付包体 */
//        TokenPayInfoPublishingToMq tokenPayInfoPublishingToMq = new TokenPayInfoPublishingToMq();
//        tokenPayInfoPublishingToMq.setCurPayLogId(currencyCustomerLogDO.getLogId());
//        tokenPayInfoPublishingToMq.setCurPayRecvTime(currencyCustomerLogDO.getCreateTime());
//        tokenPayInfoPublishingToMq.setCurPayStatus(CurrencyConstants.CURRENCY_PAY_SUCCESS);
//        tokenPayInfoPublishingToMq.setPayCurrency(priceAfterDiscount);
//        tokenPayInfoPublishingToMq.setPayId(payId);

        /* 2、发送信息到MQ */
//        try {
//            Message message = new Message(
//                    CurrencyMQConfig.SEND_TO_PAY_TOPIC,
//                    CurrencyMQConfig.TAG_PAY_STATE_SUCCESS,
//                    JSON.toJSONString(BeanOrMapConvertUtils.objectToMap(tokenPayInfoPublishingToMq), SerializerFeature.DisableCircularReferenceDetect).getBytes());
//            producer.send(message);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        return true;
    }

}
